package com.shrikanthravi.customnavigationdrawer;


import android.app.Activity;
import android.app.FragmentManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.BatteryManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.telephony.SignalStrength;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;

import java.io.File;


/**
 * A simple {@link Fragment} subclass.
 */
public class MessagesFragment extends Fragment {
    private VideoView videoView;
    private long video_total_time;     //视频时长
    private Button btn_start,btn_end,btn_soccer;
    private MediaController mediaController;
    private int phone_electric_start;  //视频开始电量
    private int phone_electric_end; //视频结束电量
    private int phone_screen_brightness;  //屏幕亮度
    final private String phone_version=android.os.Build.MODEL;  //手机版本
    private String network_type;  //网络类型
    private long start; //开始播放时刻
    private long end; //播放结束时刻
    private int video_play_total_time; //播放总时间
    private int video_rebuff_total_time; //缓冲时间
    private float rebuff_duration_proportion; //缓冲时间占比
    private int ping_avg_RTT; //网络延迟
//    Intent intent;
//    int rawLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);


//    @Override
//    public void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        getActivity().setContentView(R.layout.fragment_messages);
//        initView();
//    }

//    private void initView() {
//
//    }


    private void init() {
        mediaController = new MediaController(this.getActivity());
        //本地连接地址
        String uri = "http://172.22.106.181:8080/demo_war/video/cartoonone360p.mp4";
//        File f=new File(uri);
//        double l=  f.length();
        //网络连接地址
        //String uri = "https://gslb.miaopai.com/stream/P4DnrjGZ7PzC2LfQK9k2cAKEIw39GiixIBpIHA__.mp4";
        videoView.setVideoURI(Uri.parse(uri));
        videoView.setMediaController(mediaController);
        mediaController.setMediaPlayer(videoView);
        videoView.requestFocus();
        videoView.start();
        start=System.currentTimeMillis();

    }






//    public MessagesFragment() {
//        // Required empty public constructor
//    }
//

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_messages, container, false);
        // Inflate the layout for this fragment
        videoView= (VideoView) view.findViewById(R.id.videoView);
        btn_start= (Button) view.findViewById(R.id.btn_start);
        btn_end= (Button) view.findViewById(R.id.btn_end);
        btn_soccer=(Button) view.findViewById(R.id.btn_soccer);


        btn_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                init();
                Context context = getContext();
                phone_electric_start=Function.getPhone_electric(context);
                System.out.println(phone_electric_start);

//                System.out
            }
        });
        btn_end.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int time=videoView.getCurrentPosition()/1000;
                videoView.stopPlayback();
                end=System.currentTimeMillis();
                video_play_total_time=(int)((end-start)/1000);
                System.out.println(video_play_total_time);
                Context context = getContext();
                phone_electric_end=Function.getPhone_electric(context);

            }
        });
        btn_soccer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                FragmentManager fm = getActivity().getFragmentManager();
//                Fragment f=new NewsFragment();
//                fm.beginTransaction()
//                        //替换为TwoFragment
//                        .replace(R.id.frameLayout,f)
//                        .commit();
                Context context = getContext();
                phone_electric_end=Function.getPhone_electric(context);
                network_type=Function.getCurrentNetType(context);
                phone_screen_brightness=Function.getScreenBrightness(context);
                video_total_time=videoView.getDuration()/1000;
                video_rebuff_total_time= (int) (video_play_total_time-video_total_time);
                rebuff_duration_proportion=(float)video_rebuff_total_time/(float)video_play_total_time;
//                ping_avg_RTT=Function.get_Delay();
                String net =Function.isConnect();


                System.out.println(video_total_time);
                Bundle bundle = new Bundle();
                bundle.putString("String","需要");
//                bundle.putSerializable("key_entity", value_Entity);
                Fragment f=new NewsFragment();
                f.setArguments(bundle);
//                showFragment(MessagesFragment.this,f);
                getActivity().getSupportFragmentManager().
                        beginTransaction().replace(R.id.frameLayout,f,null).
                        addToBackStack(null).commit();
            }
        });
        return view;
    }

}
